//
//  CFindPublishNewsFlashViewController.m
//  Btpin
//
//  Created by Vic on 2019/12/16.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CFindPublishNewsFlashViewController.h"

@interface CFindPublishNewsFlashViewController ()<UITextFieldDelegate, UITextViewDelegate>
{
    UITextField *titleTextField;
    PSTextView *contentTextView;
}
@end

@implementation CFindPublishNewsFlashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}
- (void)initUI {
    self.navigationItem.title = @"发布快讯";
    self.view.backgroundColor = THEMECOLOR_OF_WHITE;
    
    titleTextField = [UIToolsControl initTextFieldWithFrame:CGRectMake(ScalePS(30), ScalePS(30), WIDTH-ScalePS(60), ScalePS(50)) placeholder:@"请输入标题" secureTextEntry:NO font:Font_15 padding:YES];
    titleTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:titleTextField];
    titleTextField.backgroundColor = COLOR_OF_WHITE;
    
    contentTextView = [[PSTextView alloc] initWithFrame:CGRectMake(30*Scale, EndY(titleTextField)+30*Scale, WIDTH-60*Scale, ScalePS(200)) leftText:@"" text:nil textColor:THEMECOLOR_OF_TEXT_333333 placeholder:@"请填写快讯内容" font:Font_15 editable:YES limitMax:(500) target:self showNumber:YES];
    contentTextView.textView.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:contentTextView];
    contentTextView.backgroundColor = COLOR_OF_WHITE;
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(ScalePS(55), EndY(contentTextView)+ScalePS(50), WIDTH-ScalePS(110), ScalePS(50)) imageName:nil target:self action:@selector(okBtnTap) title:@"发布" titleColor:COLOR_OF_WHITE];
    okBtn.titleLabel.font = Font(Font_18);
    okBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    okBtn.layer.cornerRadius = ScalePS(25);
    okBtn.layer.masksToBounds = YES;
    [self.view addSubview:okBtn];
}
- (void)okBtnTap {
    NSString *tempStr = titleTextField.text;
    [titleTextField.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    titleTextField.text = tempStr;
    if (titleTextField.text && titleTextField.text.length) {
        if (titleTextField.text.length>=5) {
            if (contentTextView.textView.text && contentTextView.textView.text.length) {
                if (contentTextView.textView.text.length>=50) {
                    [self submitData];
                } else {
                    SVPHUDError(@"快讯内容不小于50个字");
                    [contentTextView.textView becomeFirstResponder];
                }
            } else {
                SVPHUDError(PublishNewsFlashContentErrorText);
                [contentTextView.textView becomeFirstResponder];
            }
        } else {
            SVPHUDError(@"标题不小于5个字");
            [titleTextField becomeFirstResponder];
        }
    } else {
        SVPHUDError(PublishNewsFlashTitleErrorText);
        [titleTextField becomeFirstResponder];
    }
}
- (void)submitData {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setValue:titleTextField.text forKey:@"title"];
    [dic setValue:contentTextView.textView.text forKey:@"content"];
    [BaseNetManager requestWithMethod:POST path:Publish_NewsFlash_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(@"提交成功，等待审核");
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@"\n"]) {
        return NO;
    }
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (titleTextField.isFirstResponder) {
        [titleTextField resignFirstResponder];
    } else if (contentTextView.textView.isFirstResponder) {
        [contentTextView.textView resignFirstResponder];
    }
}

@end
